package com.alks.function.mapper.chengKong;

import com.alks.entity.data.entity.CpStitchProcessDetail;
import com.alks.entity.data.entity.PcStitchLotPriceDetail;
import com.alks.entity.data.entity.PcStitchProdDetail;
import com.alks.function.data.dto.chengKong.cost.*;
import com.alks.function.data.dto.finance.fincons.LotNoDto;
import com.alks.function.data.request.chengKong.PcStitchProdDetailRequest;
import com.alks.function.data.request.chengKong.cost.LotNoCutPriceRequest;
import com.alks.function.data.request.chengKong.cost.UnguidedBodyRequest;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.Mapper;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PcStitchBonusReportTempMapper
 * Package: com.alks.function.mapper.chengKong
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2024/2/23 11:13
 * @Version 1.0
 */
@Mapper
public interface PcStitchProdDetailMapper extends BaseMapper<PcStitchProdDetail> {
    List<pcStitchProdDetailDto> pcStitchProdDetailList(PcStitchProdDetailRequest request);
    Map<String,BigDecimal> pcStitchProdDetailCountSum(PcStitchProdDetailRequest request);

    /*查询针车员工产量明细*/
    List<PcStitchProdDetailDto2> getPcStitchProdDetailList(PcStitchProdDetailRequest request);

    List<PcStitchProdDetailDto2> getPcStitchProdDetailList2(PcStitchProdDetailRequest request);

    /*查询页面上的的统计数量*/
    Integer getPcStitchProdDetailOfQtyCount(PcStitchProdDetailRequest request);

    void changeYieldDetailsCfmList(@Param("request") pcStitchProdDetailDto request, @Param("username") String userName);

    void cancelYieldDetailsCfmList(@Param("request") pcStitchProdDetailDto request, @Param("username") String userName);

    void deleteYieldDetailsCfmList(@Param("request") pcStitchProdDetailDto request, @Param("username") String userName);

    /**
     * 针车选择型体
     * @param request
     */
    @Select({
            "<script>",
            "SELECT t.STYLE_NO as styleNo, t.CUT_TOOL as cutTool FROM SD_STYLE_TYPE t JOIN (SELECT COMPANY_ID,STYLE,COLOR, max(SYS_DATE) as SYS_DATE FROM CP_STITCH_PROCESS_DETAIL GROUP BY STYLE, COLOR) t1 ON t.STYLE_NO = t1.STYLE " +
                    "and t.COMPANY_ID=t1.COMPANY_ID",
            "<where>",
            " t.COMPANY_ID =#{companyId}",
            "<if test='styleNo != null and !styleNo.isEmpty()' >",
            "AND t.STYLE_NO LIKE concat('%', #{styleNo}, '%')",
            "</if>",
            "<if test='cutTool != null and !cutTool.isEmpty()' >",
            "AND t.CUT_TOOL LIKE concat('%', #{cutTool}, '%')",
            "</if>",
            "</where>",
            "GROUP BY t.STYLE_NO,t.CUT_TOOL ORDER BY t1.SYS_DATE DESC",
            "</script>",
    })
    List<Map<String,String>> getCutToolList(UnguidedBodyRequest request);

    /**
     * 针车选择颜色
     * @param styleNo
     * @return
     */
    @Select({
            "<script>",
            "SELECT  t1.STYLE as styleNO, t.COLOR_CODE, t1.COLOR AS styleColor, t1.CFM_FLAG,t.PHOTO_PATH FROM SD_STYLE_COLOR_PHOTO t RIGHT JOIN (SELECT COMPANY_ID,STYLE, COLOR, CFM_FLAG FROM CP_STITCH_PROCESS_DETAIL " +
            "GROUP BY STYLE,COLOR,CFM_FLAG) t1 ON t.STYLE_NO = t1.STYLE and t.STYLE_COLOR=t1.COLOR and t.COMPANY_ID=t1.COMPANY_ID WHERE t.STYLE_NO =#{styleNo} and t.COMPANY_ID=#{companyId} GROUP BY  t1.STYLE, t.COLOR_CODE, t1.COLOR, t1.CFM_FLAG,t.PHOTO_PATH ",
            "</script>",
    })
    List<StyleDetailToFlagDto> getStyleToColorList(String styleNo,String companyId);

    /**
     * 针车工序明细
     *
     * @param styleNo
     * @param styleColor
     * @param companyId
     */
    @Select("select SEQ_NO,PROCESS_NAME,PRICE,REMARK from CP_STITCH_PROCESS_DETAIL where STYLE=#{styleNo} and  COLOR=#{styleColor} and COMPANY_ID=#{companyId} order by SEQ_NO")
    List<CpStitchProcessDetailDto> getCpStitchProcessDetail(@Param("styleNo") String styleNo, @Param("styleColor") String styleColor, @Param("companyId") String companyId);

    /**
     * 针车工序检核
     *
     * @param styleNo
     * @param styleColor
     * @param flag
     * @param dateTime
     * @param userName
     * @param companyId
     */
    @Update("update CP_STITCH_PROCESS_DETAIL set CFM_FLAG=#{flag},CFM_DATE=#{dateTime},CFM_USER=#{userName} where STYLE=#{styleNo} and COLOR=#{styleColor} and COMPANY_ID=#{companyId}")
    void checkOutPcStitchProdDetail(@Param("styleNo") String styleNo, @Param("styleColor") String styleColor,
                                    @Param("flag") String flag, @Param("dateTime") LocalDateTime dateTime,
                                    @Param("userName") String userName, @Param("companyId") String companyId);

    /**
     * 针车工序删除
     *
     * @param styleNo
     * @param styleColor
     * @param companyId
     */
    @Delete("delete from CP_STITCH_PROCESS_DETAIL where STYLE=#{styleNo} and COLOR=#{styleColor} and COMPANY_ID=#{companyId}")
    void deletePcStitchProdDetail(String styleNo, String styleColor, String companyId);

    /**
     * 整个型体针车工序删除
     *
     * @param styleNo
     * @param companyId
     */
    @Delete("delete from CP_STITCH_PROCESS_DETAIL where STYLE=#{styleNo} and COMPANY_ID=#{companyId}")
    void deletePcStitchProdDetail2(String styleNo, String companyId);

    @Select({
            "<script>",
            "select distinct a.COMPANY_ID, a.STYLE as styleNo, a.COLOR as styleColor, b.CUT_TOOL as photoPath, c.COLOR_CODE from PC_LOT_HEAD a " +
                    "left join SD_STYLE b on a.COMPANY_ID = b.COMPANY_ID and a.STYLE = b.STYLE_NO " +
                    "left join (select distinct COMPANY_ID,STYLE_NO,STYLE_COLOR,COLOR_CODE,CFM_FLAG from SD_BOM_HEAD_NEW2) " +
                    "c on a.COMPANY_ID = c.COMPANY_ID and a.STYLE = c.STYLE_NO and a.COLOR = c.STYLE_COLOR " +
                    "where a.STYLE is not null and a.COLOR is not null " +
                    "and not exists (select 1 from CP_STITCH_PROCESS_DETAIL b where a.COMPANY_ID = b.COMPANY_ID and a.STYLE = b.STYLE and a.color = b.COLOR ) " +
                    "and a.COMPANY_ID =#{companyId}",
            "<if test='styleNo !=null and !styleNo.isEmpty()' >",
            "AND a.STYLE LIKE concat('%', #{styleNo}, '%')",
            "</if>",
            "<if test='cutTool !=null and !cutTool.isEmpty()' >",
            "AND b.CUT_TOOL LIKE concat('%', #{cutTool}, '%')",
            "</if>",
            "<if test='color !=null and !color.isEmpty()' >",
            "AND c.COLOR LIKE concat('%', #{color}, '%')",
            "</if>",
            "</script>",
        })
    List<StyleDetailToFlagDto> getStyleToColorToCutToolList(UnguidedBodyRequest request);

    /**
     * 查询是否已存在针车工序资料
     *
     * @param style
     * @param color
     * @param companyId
     * @return
     */
    @Select("select count(1) from CP_STITCH_PROCESS_DETAIL where STYLE=#{style} and COLOR=#{color} and COMPANY_ID =#{companyId}")
    Integer getStitchStyleToColor(String style, String color, String companyId);
    /**
     * 针车工序新增
     * @param list
     */
    @Insert({
            "<script>",
            "INSERT INTO CP_STITCH_PROCESS_DETAIL(COMPANY_ID, STYLE, SEQ_NO, PROCESS_NAME, PRICE, COLOR, SYS_DATE, SYS_USER, CFM_FLAG, CFM_DATE, CFM_USER, REMARK)",
            "VALUES",
            "<foreach collection='list' item='t' separator=','>",
            "(#{t.companyId}, #{t.style}, #{t.seqNo}, #{t.processName}, #{t.price}, #{t.color}, #{t.sysDate}, #{t.sysUser}, #{t.cfmFlag}, #{t.cfmDate}, #{t.cfmUser}, #{t.remark})",
            "</foreach>",
            "</script>",
    })
    void addCpStitchProcessDetail(List<CpStitchProcessDetail> list);

    /**
     * 针车指令查询
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "SELECT t.LOT_NO,t.STYLE,t.COLOR,t.COLOR_CODE,t1.CFM_FLAG FROM (SELECT COMPANY_ID,LOT_NO, STYLE, COLOR, COLOR_CODE FROM PC_LOT_HEAD AS S WHERE CHECK_FLAG = 'Y'" +
            "AND CFM_FLAG= 'Y'AND NOT EXISTS (SELECT 1 FROM PC_STITCH_LOT_PRICE_DETAIL AS P WHERE P.STYLE = S.STYLE AND P.LOT_NO = S.LOT_NO AND P.COLOR = S.COLOR))t " +
            "LEFT JOIN(select COMPANY_ID,STYLE,COLOR,CFM_FLAG from CP_STITCH_PROCESS_DETAIL where CFM_FLAG='Y' GROUP BY STYLE,COLOR)t1 on t.STYLE=t1.STYLE and t.COLOR=t1.COLOR and t.COMPANY_ID = t1.COMPANY_ID",
            "<where>",
            "and t.COMPANY_ID=#{companyId}",
            "<if test='styleNo != null and !styleNo.isEmpty()'>",
            "and t.STYLE LIKE concat('%',#{styleNo},'%')",
            "</if>",
            "<if test='lotNo != null and !lotNo.isEmpty()'>",
            "and t.LOT_NO LIKE concat('%',#{lotNo},'%')",
            "</if>",
            "</where>",
            "<if test='sortNo != null and sortNo==1'>",
            "order by t.LOT_NO asc",
            "</if>",
            "<if test='sortNo != null and sortNo==2'>",
            "order by t.LOT_NO desc",
            "</if>",
            "<if test='sortNo != null and sortNo==3'>",
            "order by t.STYLE asc",
            "</if>",
            "<if test='sortNo != null and sortNo==4'>",
            "order by t.STYLE desc",
            "</if>",
            "</script>",
    })
    List<LotNoDetailDto> getStitchLotNoDetail(LotNoCutPriceRequest request);

    /**
     * 删除针车指令工序
     *
     * @param lotNo
     * @param companyId
     */
    @Delete("delete from PC_STITCH_LOT_PRICE_DETAIL where LOT_NO=#{lotNo} and COMPANY_ID=#{companyId}")
    void deletePcStitchLotProdDetail(String lotNo, String companyId);

    /**
     * 针车指令工序新增
     * @param list
     */
    @Insert({
            "<script>",
            "INSERT INTO PC_STITCH_LOT_PRICE_DETAIL (COMPANY_ID, STYLE, LOT_NO, SEQ_NO, PROCESS_NAME, MACHINE_CODE, MACHINE_NAME, WORK_TIME, ALLOWANCE_RATE, STD_TIME, STD_PROD, PERSON_REQ, DAY_PROD, PRICE, REMARK, SYS_DATE, SYS_USER, COLOR, CFM_USER, CFM_DATE, CFM_FLAG)",
            "VALUES",
            "<foreach collection='list' item='t' index='index' separator=','>",
            "(#{t.companyId}, #{t.style}, #{t.lotNo}, #{t.seqNo}, #{t.processName}, #{t.machineCode}, #{t.machineName}, #{t.workTime}, #{t.allowanceRate}, #{t.stdTime}, #{t.stdProd}, #{t.personReq}, #{t.dayProd}, #{t.price}, #{t.remark}, #{t.sysDate}, #{t.sysUser}, #{t.color}, #{t.cfmUser}, #{t.cfmDate}, #{t.cfmFlag})",
            "</foreach>",
            "</script>"
    })
    void addPcStitchLotProdDetail(List<PcStitchLotPriceDetail> list);

    @Select({
            "<script>",
            "SELECT t1.LOT_NO,t1.STYLE,t1.COLOR,t1.CFM_FLAG,t1.SYS_DATE,t.COLOR_CODE from PC_LOT_HEAD t join (select COMPANY_ID,LOT_NO, STYLE, COLOR , CFM_FLAG, MIN(SYS_DATE) SYS_DATE from PC_STITCH_LOT_PRICE_DETAIL " +
                    "group by COMPANY_ID,LOT_NO, STYLE, COLOR)t1 on t.STYLE=t1.STYLE and t.COLOR=t1.COLOR and t.LOT_NO=t1.LOT_NO and t.COMPANY_ID=t1.COMPANY_ID ",
            "<where>",
            "and t.COMPANY_ID=#{companyId}",
            "<if test='styleNo != null and !styleNo.isEmpty()'>",
            "and t1.STYLE LIKE concat('%', #{styleNo}, '%')",
            "</if>",
            "<if test='lotNo != null and !lotNo.isEmpty()'>",
            "and t1.LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='inTime != null and outTime != null'>",
            "and t1.SYS_DATE BETWEEN #{inTime} AND #{outTime}",
            "</if>",
            "<if test='inTime != null and outTime == null'>",
            "and t1.SYS_DATE &gt;= #{inTime}",
            "</if>",
            "<if test='outTime != null and inTime == null'>",
            "and t1.SYS_DATE &lt;= #{outTime}",
            "</if>",
            "</where>",
            "<if test='sortNo != null and sortNo==1'>",
            "order by t1.LOT_NO asc",
            "</if>",
            "<if test='sortNo != null and sortNo==2'>",
            "order by t1.LOT_NO desc",
            "</if>",
            "<if test='sortNo != null and sortNo==3'>",
            "order by t1.STYLE asc",
            "</if>",
            "<if test='sortNo != null and sortNo==4'>",
            "order by t1.STYLE desc",
            "</if>",
            "</script>",
    })
    List<LotNoDetailDto> getLotNoStitchDetail(LotNoCutPriceRequest request);

    /**
     * 针车型体单价明细
     *
     * @param lotNo
     * @param companyId
     * @return
     */
    @Select("select SEQ_NO,PROCESS_NAME,PRICE,REMARK from PC_STITCH_LOT_PRICE_DETAIL where LOT_NO=#{lotNo} and COMPANY_ID=#{companyId} order by SEQ_NO")
    List<CpStitchProcessDetailDto> getLotNoStitchDetailList(String lotNo, String companyId);

    /*指令查询针车*/
    LotNoDto queryPcStitchLotPriceDetail(@Param("companyId") String companyId, @Param("request") LotNoDto request);

    /*指令针车检核1、取消检核2*/
    int updatePcStitchLotPriceDetail(@Param("companyId") String companyId, @Param("request") LotNoDto request);

    /*针车工序回显-查询*/
    List<CpStitchProcessDetailDto> getAllCpStitchProcessDetail(String companyId,String process);

    /**
     * 获取单指令指定日期的已缴库最小值
     * @param lotNo 指令号
     * @param slipDate 缴库日期
     * @param companyId 公司ID
     */
    BigDecimal thresholdByLotAndDate(@Param("lotNo") String lotNo, @Param("slipDate") LocalDate slipDate, @Param("companyId") String companyId);
}
